package top.shiyiri.try_;

import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author Aunean
 * @date 2022/1/15 21:32
 */
public class TryTest {
    /*
    Java9新特性：try操作的升级
     */
    public static void main(String[] args) {
        //Java8之前的资源关闭
/*        InputStreamReader reader = null;
        try {
            reader = new InputStreamReader(System.in);

            char[] cbuf = new char[20];
            int len;

            while ((len = reader.read(cbuf)) != -1) {
                String s = new String(cbuf, 0, len);
                System.out.println(s);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert reader != null;
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/


        //Java8中资源关闭操作：Java8中，可以实现资源的自动关闭
        //要求自动关闭的资源的初始化必须放在try的一对小括号中
/*        try(InputStreamReader reader = new InputStreamReader(System.in)) {
            char[] cbuf = new char[20];
            int len;

            while ((len = reader.read(cbuf)) != -1) {
                String s = new String(cbuf, 0, len);
                System.out.println(s);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }*/


        //Java9中资源关闭操作：需要自动关闭的资源的实例化可以放在try的小括号外
        //此时的资源属性是常量，不可进行更改
        InputStreamReader reader = new InputStreamReader(System.in);
        try(reader) {
            char[] cbuf = new char[20];
            int len;

            while ((len = reader.read(cbuf)) != -1) {
                String s = new String(cbuf, 0, len);
                System.out.println(s);
            }
            //reader = null;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
